package com.aaa.aop;

import com.aaa.entity.ResultMsg;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Aspect
public class OauthToken {

  @Around("execution(public * org..springframework.security.oauth2.provider.endpoint.TokenEndpoint.postAccessToken(..))")
public Object tokenHandler(ProceedingJoinPoint joinPoint) throws Throwable {
      Object[] args = joinPoint.getArgs();
      Object proceed = joinPoint.proceed();//执行接口
      //判断 授权方式 ：授权码

      Map<String,Object> map = (Map<String, Object>) args[1];
      String grant_type = map.get("grant_type").toString();
      if ("authorization_code".equals(grant_type)){
          return proceed;
      }else {
          //封装令牌结果
          Object body=((ResponseEntity)proceed).getBody();
          System.out.println(body);
          //{code:,message:,data:}
          ResultMsg resultMsg = ResultMsg.success(body);
          return new ResponseEntity<ResultMsg>(resultMsg, HttpStatus.OK);
      }

  }



}
